package com.qf.service.impl;

import com.qf.exception.CustomerException;
import com.qf.mapper.BuildingMapper;
import com.qf.mapper.RoomMapper;
import com.qf.pojo.Building;
import com.qf.service.BuildingService;
import com.qf.vo.BuildingVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zdl
 * @date 2025/2/15
 */
@Service
public class BuildingServiceImpl implements BuildingService {
    @Resource
    private BuildingMapper buildingMapper;
    @Resource
    private RoomMapper roomMapper;

    @Override
    public List<Building> findAllBuilding() {
        return buildingMapper.selectAllBuilding();
    }

    @Override
    public int addBuilding(Building building) {
        System.out.println(building);
        //查询添加的建筑是否存在
        Building building1 = buildingMapper.selectBuildingByBuildingNameAndBuildingLocation(building.getBuildingName(), building.getBuildingLocation());
        if (building1 != null) {
            throw new CustomerException("60001", "添加的楼栋已存在");
        }
        return buildingMapper.insertBuilding(building);
    }

    @Override
    public int delBuildingBatch(List<Integer> bids) {
        //删除前判断楼层是否有房间
        for (Integer bid : bids) {
            BuildingVo buildingVo = buildingMapper.selectBuildingWithRoomByBid(bid);
            if (buildingVo != null){
                throw  new CustomerException("60002","该楼层还存在房间，清闲删除房间后再删除楼层");
            }
        }
        return buildingMapper.delBuildingBatch(bids);
    }
}
